package com.example.prom.qianduoduo.y2025.handler;

import com.alibaba.excel.EasyExcel;
import com.example.prom.qianduoduo.y2025.model.MedicineInfo;
import com.example.prom.qianduoduo.y2025.model.MedicineInstruction;
import com.example.prom.qianduoduo.y2025.model.SacinfoModel;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description
 *
 * @author DangWen
 */
public class Test {
    public static void main(String[] args) {
        List<MedicineInfo> medicineInfos = readExcel("D:\\dangwen\\钱多多\\同事\\药品目录.xlsx");
        List<String> originNames = medicineInfos.stream().map(MedicineInfo::getMedicineName).collect(Collectors.toList());
//        List<MedicineInfo> collect = sacinfoModelList.stream().filter(x -> x.getProductionEnterprise() != null && x.getModificationDate() != null).collect(Collectors.toList());
//        System.out.println(collect.size());
        List<String> names = new ArrayList<>();
        List<MedicineInfo> allMedicine = new ArrayList<>();
        List<MedicineInstruction> allNoData = new ArrayList<>();
        YaozhMergeFileHandler.readOneFile("D:\\dangwen\\钱多多\\同事\\mergedFile.xlsx", allMedicine, allNoData);
        List<String> names1 = allMedicine.stream().map(MedicineInfo::getMedicineName).collect(Collectors.toList());
        List<String> names2 = allNoData.stream().map(MedicineInstruction::getMedicineName).collect(Collectors.toList());
        names.addAll(names1);
        names.addAll(names2);
        HashSet<String> strings = new HashSet<>(names1);
        strings.addAll(names2);
        System.out.println(strings.size());
        // 找出originNames 和 names 不一样的数据
        List<String> diffNames = originNames.stream().filter(name -> !names.contains(name)).collect(Collectors.toList());
        System.out.println(diffNames.size());
        List<String> diffNames2 = names.stream().filter(name -> !originNames.contains(name)).collect(Collectors.toList());
        System.out.println(diffNames2.size());
        List<MedicineInfo> filteredMedicine = medicineInfos.stream().filter(x -> diffNames.contains(x.getMedicineName())).collect(Collectors.toList());
        System.out.println(filteredMedicine.size());
        writeExcel(filteredMedicine, "D:\\dangwen\\钱多多\\同事\\药品目录过滤出已爬数据.xlsx");

    }
    private static List<MedicineInfo> readExcel(String filePath) {
        return EasyExcel.read(filePath)
                .head(MedicineInfo.class)
                .sheet()
                .doReadSync();
    }

    private static void writeExcel(List<MedicineInfo> data, String outputPath) {
        EasyExcel.write(outputPath, MedicineInfo.class)
                .sheet()
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy())
                .doWrite(data);
        System.out.println("已生成差异文件：" + outputPath);
    }

}
